﻿using Microsoft.Xna.Framework;
using GlobalGameClasses;
using System.Diagnostics;
using DrawableObjects.Actors.Aircrafts;
using Microsoft.Xna.Framework.Audio;
using LightEffects;

namespace DrawableObjects.Actors.Projectiles
{
    public abstract class Projectile : Actor
    {
        /// <summary>
        /// This class managaes the collision between projectiles and aircrafts and sets the amout of damage depending on
        /// difficulty of the game and which kind of weaponsystem produces the projectiles.
        /// </summary>
        protected string strTextureModel;

        protected int baseDamage
        {
            get;
            private set;
        }

        protected Vector2 direction;

        public PointLight light
        {
            get;
            protected set;
        }

        protected string explosionSound;

        public ISoundPlayer soundPlayer = GlobalGameClasses.SirianSoundPlayer.GetSoundPlayer();

        /// <summary>
        /// lets the projectiles fly into the correct direction
        /// </summary>
        public override void Update()
        {
            base.Update();
            this.moveInDirection(direction * speed);
        }

        /// <summary>
        /// returns the amount of damage. if the game difficulty is very hard(wissenschaftler)
        /// the playeraircraft will get oneshoted.
        /// </summary>
        public float getDamage()
        {
            if (this.owner == Owner.ENEMY)
            {
                if (GlobalGameValues.Values.difficulty == Difficulty.DIFFICULTY_VERY_HARD) {
                    return baseDamage * 9000;
                }
                return (float)baseDamage;
            }
            else
            {
                return (float)baseDamage / GlobalGameClasses.GlobalGameValues.Values.difficulty.multiplier;
            }
        }

        public abstract Projectile Clone(Vector2 position, Vector2 direction);

        protected Projectile(float speed, int baseDamage, string strTextureModel, Matrix worldMatrix, float scaleFactor, float boundingFactor, string explosionSound)
            : base(strTextureModel, worldMatrix, scaleFactor, boundingFactor)
        {
            this.speed = speed;
            this.baseDamage = baseDamage;
            this.explosionSound = explosionSound;
            this.faceMoveDirection = true;
        }

        /// <summary>
        /// implements the collision between projectiles and aircrafts
        /// adds the sounds when a collision occures
        /// </summary>
        public override void collide(Actor actor2)
        {
            Debug.Assert(actor2 != null);
            if (actor2 is Aircraft)
            {
                if (actor2.owner != this.owner && !((Aircraft)actor2).aircraftAttributes.Contains(DrawableObjects.Actors.Aircrafts.Aircraft.AircraftAttributes.Transparent_Aircraft))
                {
                    world.removeActor(this);
                    Sound_OnDestroy.Volume = GlobalGameValues.Values.volume_soundEffect / 100f;
                    soundPlayer.PlaySound(Sound_OnDestroy);
                }
            }
        }

        public override void Initialize()
        {
            SoundEffect onDestroy;
            if (!(GlobalGameValues.Values.content.GameSounds.TryGetValue(explosionSound, out onDestroy)))
            {
                throw new System.ArgumentException("Couldn't find Soundeffect: " + explosionSound);
            }
            if (onDestroy != null)
            {
                Sound_OnDestroy = onDestroy.CreateInstance();
            }
            base.Initialize();
        }
    }
}

